package org.jeecg.modules.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.knowledge.entity.PhilKnowledge;
import org.jeecg.modules.knowledge.mapper.PhilKnowledgeMapper;
import org.jeecg.modules.knowledge.service.IPhilKnowledgeService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 知识
 * @Author: jeecg-boot
 * @Date:   2020-12-20
 * @Version: V1.0
 */
@Service
public class PhilKnowledgeServiceImpl extends ServiceImpl<PhilKnowledgeMapper, PhilKnowledge> implements IPhilKnowledgeService {

    @Autowired
    private PhilKnowledgeMapper philKnowledgeMapper;

    @Override
    public Page<PhilKnowledge> getSortKnowledgeByType(Page<PhilKnowledge> page, String knowledgeType) {
        List<PhilKnowledge> list = philKnowledgeMapper.selectList2(page,knowledgeType);
        List<PhilKnowledge> lis = list.stream()
                .sorted(Comparator.comparing(PhilKnowledge::getCreateTime).reversed())
                .sorted(Comparator.comparing(PhilKnowledge::getKnowledgeColumnTop).reversed())
                .collect(Collectors.toList());
        return page.setRecords(lis);
    }

    @Override
    public void incrReadNum(String id) {
        philKnowledgeMapper.incrReadNum(id);
    }

    @Override
    public Page<PhilKnowledge> getSortKnowledgeByKeyword(Page<PhilKnowledge> page, String keyword) {
        List<PhilKnowledge> list = philKnowledgeMapper.getKnowledgeListByKeyword(keyword);
        List<PhilKnowledge>lis =
                list.stream()
                        .sorted(Comparator.comparing(PhilKnowledge::getCreateTime).reversed())
                        .collect(Collectors.toList());
        return page.setRecords(lis);
    }

    @Override
    public void insertKeywordKnowledge(PhilKnowledge philKnowledge) {
        String[] split = philKnowledge.getKnowledgeKeywords().split(",");
        List<String> list = Arrays.asList(split);
        philKnowledgeMapper.insertKeyword(list,philKnowledge.getId());
    }

    @Override
    public void delKeywordKnowledge(String id) {
        philKnowledgeMapper.delKeywordKnowledge(id);
    }

    @Override
    public int count(DateTime monday, DateTime sunday) {
        return philKnowledgeMapper.count(monday,sunday);
    }
}
